package leetcodecompetition.D373;

import org.junit.jupiter.api.Test;
import tool.s;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

/**
 * @Author: Siantar
 * @Date: 2023-11-26-11:08
 * @Description: 1.0
 */
public class T100142 {
    public int[] lexicographicallySmallestArray(int[] nums, int limit) {
        int n = nums.length;
        Integer[] indexs = new Integer[n];
        for (int i = 0; i < n; i++) {
            indexs[i] = i;
        }
        Arrays.sort(indexs, (a, b) -> nums[a] - nums[b]);
        s.print(indexs);
        PriorityQueue<Integer> pq = new PriorityQueue<Integer>((a, b) -> a - b);
        List<Integer> list = new ArrayList<>();
        int preValue = -1;
        for (int i = 0; i < n; i++) {
            int index = indexs[i];
            int value = nums[index];
            System.out.println(index + "----" + value);
            if (preValue != -1 && Math.abs(value - preValue) > limit) {
                int m = list.size();
                for (int j = 0; j < m; j++) {
                    int d = pq.poll();
                    nums[d] = list.get(j);
                    System.out.println(d + " " + list.get(j));
                }
                list.clear();
            }
            pq.offer(index);
            list.add(value);
            preValue = value;
        }
        int m = list.size();
        for (int j = 0; j < m; j++) {
            int d = pq.poll();
            nums[d] = list.get(j);
            System.out.println(d + " " + list.get(j));
        }
        return nums;
    }

    @Test
    public void test() {
        s.print(lexicographicallySmallestArray(new int[]{1, 5, 3, 9, 8}, 2));
    }
}
